package com.ronsai.umtrack.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.obs.services.ObsClient;
import com.obs.services.model.HeaderResponse;
import com.ronsai.umtrack.R;
import com.ronsai.umtrack.base.BaseActivity;
import com.ronsai.umtrack.base.BasePresenter;
import com.ronsai.umtrack.base.BaseView;
import com.ronsai.umtrack.util.FileUtils;
import com.ronsai.umtrack.util.ToastUtil;
import com.ronsai.umtrack.util.UriUtils;
import com.ronsai.umtrack.util.permission.PermissionCallback;
import com.ronsai.umtrack.util.permission.PermissionUtil;
import com.yanzhenjie.permission.Permission;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

public class HuaweiCloudActivity extends BaseActivity {

    @BindView(R.id.select_file_tv)
    TextView selectFileTv;
    @BindView(R.id.put_object_tv)
    TextView putObjectTv;
    @BindView(R.id.get_object_tv)
    TextView getObjectTv;
    @BindView(R.id.image)
    ImageView image;

    private static final String url_cloud = "https://obs-8766.obs.cn-south-1.myhuaweicloud.com/";
    private static final String endPoint = "obs.cn-south-1.myhuaweicloud.com";
    private static final String ak = "WZ7O1UNL4SDOJE8BAJST";
    private static final String sk = "A2btDXVVn2V7pSW40HHthP3OxzOup4yQT9G8MTr3";
    private ObsClient obsClient;

    private String filePath;
    private String requestId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_huawei_cloud;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected BaseView createView() {
        return null;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        // 初始化OBS客户端
        obsClient = new ObsClient(ak, sk, endPoint);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) return;
        switch (requestCode) {
            case 100:
                Uri selectImageUri = data.getData();
                // 获取真实的路径
                try {
                    filePath = UriUtils.getPath(mContext, selectImageUri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TextUtils.isEmpty(filePath) || !new File(filePath).exists()) {
                    ToastUtil.showShortToast("无法加载该图片");
                    return;
                }
                ToastUtil.showLongToast(filePath);
                //Glide.with(mContext).load(filePath).into(image);
                break;
        }
    }

    @OnClick({R.id.select_file_tv, R.id.put_object_tv, R.id.get_object_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.select_file_tv:
                PermissionUtil.requestPermission(mContext, new PermissionCallback() {

                    @Override
                    public void onGranted() {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_PICK);
                        startActivityForResult(intent, 100);
                    }

                    @Override
                    public void onDenied() {

                    }
                }, Permission.Group.STORAGE);
                break;
            case R.id.put_object_tv:
                new Thread(() -> {
                    try {
                        HeaderResponse response = obsClient.putObject("obs-8766", FileUtils.getFileMD5ToString(filePath), new File(filePath));
                        Log.d("obs", "putObject: " + response);
                        requestId = response.getRequestId();
                        Log.d("obs", "requestId: " + response.getRequestId());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
                break;
            case R.id.get_object_tv:
                Glide.with(mContext).load(url_cloud + requestId).into(image);
                break;
        }
    }
}
